package com.google.demo.Activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.demo.R;

public class MainActivity extends Activity {
	private MainActivity _me = this;
	LinearLayout linearlayout_maincontrol;
	RelativeLayout relativelayout_header;
	FrameLayout framelayout_main;
	TextView mTextViewHeader;
	private Button mButton_left,mButton_right;
	private View left_view,right_view;
	public static List<MainActivity> stack = new ArrayList<MainActivity>();
	private static boolean isLoading = false;
	private UIBottomButtonsHelper uiButtonsHelper;
	
	public LinearLayout getLinearLayoutMainControl() {
		return linearlayout_maincontrol;
	}
	
	public void setLinearLayoutMainControl(LinearLayout linearlayout_maincontrol) {
		this.linearlayout_maincontrol = linearlayout_maincontrol;
	}
	
	public RelativeLayout getRelativeLayoutHeader() {
		return relativelayout_header;
	}

	public void setRelativeLayoutHeader(RelativeLayout relativelayout_header) {
		this.relativelayout_header = relativelayout_header;
	}
	
	public FrameLayout getFrameLayoutMain() {
		return framelayout_main;
	}

	public void setFrameLayoutMain(FrameLayout framelayout_main) {
		this.framelayout_main = framelayout_main;
	}
	
	public TextView getTextViewHeader() {
		return mTextViewHeader;
	}

	public void setTextViewHeader(TextView textViewHeader) {
		this.mTextViewHeader = textViewHeader;
	}
	
	public UIBottomButtonsHelper getUiButtonsHelper() {
		return uiButtonsHelper;
	}

	public void setUiButtonsHelper(UIBottomButtonsHelper uiButtonsHelper) {
		this.uiButtonsHelper = uiButtonsHelper;
	}
	public void setmButton_left(Button mButton_left) {
		this.mButton_left = mButton_left;
	}
	public Button getmButton_left() {
		return mButton_left;
	}
	public void setmButton_right(Button mButton_right) {
		this.mButton_right = mButton_right;
	}
	public Button getmButton_right() {
		return mButton_right;
	}
	public TextView getmTextViewHeader() {
		return mTextViewHeader;
	}
	public void setLeft_view(View left_view) {
		this.left_view = left_view;
	}
	public View getLeft_view() {
		return left_view;
	}
	public void setRight_view(View right_view) {
		this.right_view = right_view;
	}
	public View getRight_view() {
		return right_view;
	}
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		MainActivity.stack.add(this);
		setContentView(R.layout.main);
		
		linearlayout_maincontrol = (LinearLayout) findViewById(R.id.linearlayout_maincontrol);
		relativelayout_header = (RelativeLayout) findViewById(R.id.relativelayout_header);
		framelayout_main = (FrameLayout) findViewById(R.id.framelayout_main);
		mTextViewHeader = (TextView) relativelayout_header.findViewById(R.id.textview_header);
		mButton_left=(Button) relativelayout_header.findViewById(R.id.btn_left_header);
		mButton_right=(Button) relativelayout_header.findViewById(R.id.btn_right_header);
		left_view=(View)relativelayout_header.findViewById(R.id.view_left_header);
		right_view=(View)relativelayout_header.findViewById(R.id.view_right_header);
		setUiButtonsHelper(new UIBottomButtonsHelper(this));
		getUiButtonsHelper().updateFooterButton();
		isLoading = false;
	}
	
	public void onScanButtonClick(View view) {
		if (UIBottomButtonsHelper.CURRENT_SCREEN_OF_4_MAINSCREEN != UIBottomButtonsHelper.SCAN_SCREEN && isLoading == false) {
			isLoading = true;
			removeAllStackActivity();
			Intent intent = new Intent(_me, InputDataActivity.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			overridePendingTransition(0, 0);
			UIBottomButtonsHelper.CURRENT_SCREEN_OF_4_MAINSCREEN = UIBottomButtonsHelper.SCAN_SCREEN;
			finish();
		}
	}
	
	public void onHistoriesButtonClick(View view) {
		if (UIBottomButtonsHelper.CURRENT_SCREEN_OF_4_MAINSCREEN != UIBottomButtonsHelper.HISTORY_SCREEN && isLoading == false) {
			isLoading = true;
			removeAllStackActivity();
			Intent intent = new Intent(_me, HistoryActivity.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			overridePendingTransition(0, 0);
			UIBottomButtonsHelper.CURRENT_SCREEN_OF_4_MAINSCREEN = UIBottomButtonsHelper.HISTORY_SCREEN;
			finish();
		}
	}
	
	public void onSettingButtonClick(View view) {
		if (UIBottomButtonsHelper.CURRENT_SCREEN_OF_4_MAINSCREEN != UIBottomButtonsHelper.SETTING_SCREEN && isLoading == false) {
			isLoading = true;
			removeAllStackActivity();
			Intent intent = new Intent(_me, SettingActivity.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			overridePendingTransition(0, 0);
			UIBottomButtonsHelper.CURRENT_SCREEN_OF_4_MAINSCREEN = UIBottomButtonsHelper.SETTING_SCREEN;
			finish();
		}
	}
	
	protected void removeAllStackActivity()
	{
		for(int i= 0 ; i < MainActivity.stack.size(); i++)
			if(MainActivity.stack.get(i) != this)
				MainActivity.stack.get(i).finish();
		MainActivity.stack = null;
		MainActivity.stack = new ArrayList<MainActivity>();
		MainActivity.stack.add(this);
	}
	
	@Override
	protected void onDestroy() {
		System.gc();
		super.onDestroy();
		MainActivity.stack.remove(this);
	}
}